var GoodsClass = function() {

	var left_margin_step = 15;
	$("#qrygoodsClass").bind(
			'click',
			function() {
				var myts = new Date();
				//var formStr = $('#prodclass_filter_form').serialize();
				var formStr = $(this).parents("form").find("#prodCl_filter").serialize();
				var trigger = $(this);
				var triggerId = $(this).attr('id');
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/qryMarClass?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#productUnitPicker").find(".tree_list1").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var prodClassPk = obj.pk_marbasclass;
								var prodClassName = obj.name;
								var a = $("<a></a>").attr("href", "#").addClass(
										"category_trigger1").attr("margin", 0);
								var inputObj = $("<input/>")
												.attr("type", "checkbox")
												.attr("id", "pk_marbasclass_intree")
												.attr("name", "pk_marbasclass_intree")
												.val(obj.pk_marbasclass+"_"+obj.name)
												.addClass(obj.pk_marbasclass);
								$(inputObj).click(function(){
									var checkValue = $(this).val();
									var checkProdClassPk = checkValue.substr(0,20);
									var checkProdClassName = checkValue.substr(21);
									if(this.checked){
										var checkBox = $("<input/>")
														.attr("type", "checkbox")
														.val(checkValue)
														.attr("style","margin-left:0px;px;margin-top:0px").attr("checked","checked");
										$(checkBox).click(function(){
											var checkValue = $(this).val();
											var checkProdClassPk = checkValue.substr(0,20);
											var checkProdClassName = checkValue.substr(21);
											var objs = $("."+checkProdClassPk);
											if(objs.length > 0){
												objs[0].checked = false;
											}
											$(this).parent().remove();
											var divHeight = $("#sel_prodclass")[0].offsetHeight;
											var allChild = $("#sel_prodclass").children();
											var lastChildHeight = allChild[allChild.length - 1].offsetTop;	
											//设置已选产品去高度
											var objDialogs = $(".ui-drop_dialog_content");
											var idxObj;
											for(idxObj=0;idxObj<objDialogs.length;idxObj++){
												var objDialog = $(objDialogs[idxObj]);
												if(objDialog.is(":visible")){
													objDialog.attr("style","height:"+ lastChildHeight +"px");
													break;
												}
											}
											$($(".border.lft")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
											$($(".border.rit")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
											$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
											$(".border.lft").attr("style","height:"+ lastChildHeight +"px");
											$(".border.rit").attr("style","height:"+ lastChildHeight +"px");
											$("#sel_prodclass").attr("style","height:"+ lastChildHeight +"px");
										});
										var labelProd = $("<label />").html(checkProdClassName);
										var div = $("<div></div>").attr("id", checkProdClassPk).attr("name", checkProdClassPk).attr("style", "float:left;display:inline").append(checkBox).append(labelProd);										
										$("#sel_prodclass").append(div);
									}else{									
										$("#"+checkProdClassPk).remove();
									}
									var allChild = $("#sel_prodclass").children();
									var lastChildHeight = allChild[allChild.length - 1].offsetTop;									
									$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
					                $(".border.lft").attr("style","height:"+ lastChildHeight +"px");
					                $(".border.rit").attr("style","height:"+ lastChildHeight +"px");
					                $("#sel_prodclass").attr("style","height:"+ lastChildHeight +"px");	
								});
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>").val(obj.pk_marbasclass).attr(
										"type", "hidden");	
								var div = $("<div></div>")
										.addClass("tree_category1").append(a)
										.append(inputObj).append(label).append(inputObj1);												
								$("#productUnitPicker").find(".tree_list1")
										.append(div);
							}
							
						}
					}
				});                                                  
				var position = $(this).offset();
				position.top += 25;
				$("#productUnitPicker").slideDown("normal");
				$("#productUnitPicker").offset(position);
			});
			
	
	$("#filterProdClass").bind(
			'click',
			function() {
				var myts = new Date();
				//var formStr = $('#prodclass_filter_form').serialize();
				var formStr = $(this).parents("form").find("#prodCl_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/filterMarClass?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#productUnitPicker").find(".tree_list1").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var prodClassPk = obj.pk_marbasclass;
								var prodClassName = obj.name;
								var a = $("<a></a>").attr("href", "#").addClass(
										"category_trigger1").attr("margin", 0);								
								var inputObj = $("<input/>")
												.attr("type", "checkbox")
												.attr("id", "pk_marbasclass_intree")
												.attr("name", "pk_marbasclass_intree")
												.val(obj.pk_marbasclass+"_"+obj.name)
												.addClass(obj.pk_marbasclass);
								$(inputObj).click(function(){
									var checkValue = $(this).val();
									var checkProdClassPk = checkValue.substr(0,20);
									var checkProdClassName = checkValue.substr(21);
									if(this.checked){
										var checkBox = $("<input/>")
														.attr("type", "checkbox")
														.val(checkValue)
														.attr("style","margin-left:0px;px;margin-top:0px").attr("checked","checked");
										$(checkBox).click(function(){
											var checkValue = $(this).val();
											var checkProdClassPk = checkValue.substr(0,20);
											var checkProdClassName = checkValue.substr(21);
											var objs = $("."+checkProdClassPk);
											if(objs.length > 0){
												objs[0].checked = false;
											}
											$(this).parent().remove();
											var divHeight = $("#sel_prodclass")[0].offsetHeight;
											var allChild = $("#sel_prodclass").children();
											var lastChildHeight = allChild[allChild.length - 1].offsetTop;											
											//设置已选产品去高度
											var objDialogs = $(".ui-drop_dialog_content");
											var idxObj;
											for(idxObj=0;idxObj<objDialogs.length;idxObj++){
												var objDialog = $(objDialogs[idxObj]);
												if(objDialog.is(":visible")){
													objDialog.attr("style","height:"+ lastChildHeight +"px");
													break;
												}
											}
											$($(".border.lft")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
											$($(".border.rit")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
											$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
											$(".border.lft").attr("style","height:"+ lastChildHeight +"px");
											$(".border.rit").attr("style","height:"+ lastChildHeight +"px");
											$("#sel_prodclass").attr("style","height:"+ lastChildHeight +"px");											
										});
										var labelProd = $("<label />").html(checkProdClassName)
										var div = $("<div></div>").attr("id", checkProdClassPk).attr("name", checkProdClassPk).attr("style", "float:left;display:inline").append(checkBox).append(labelProd);										
										$("#sel_prodclass").append(div);;
									}else{										
										$("#"+checkProdClassPk).remove();
									}
									var allChild = $("#sel_prodclass").children();
									var lastChildHeight = allChild[allChild.length - 1].offsetTop;									
									$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
					                $(".border.lft").attr("style","height:"+ lastChildHeight +"px");
					                $(".border.rit").attr("style","height:"+ lastChildHeight +"px");
					                $("#sel_prodclass").attr("style","height:"+ lastChildHeight +"px");																			
								});
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>").val(obj.pk_marbasclass).attr(
										"type", "hidden");	
								var div = $("<div></div>")
										.addClass("tree_category1").append(a)
										.append(inputObj).append(label).append(inputObj1);												
								$("#productUnitPicker").find(".tree_list1")
										.append(div);
							}
							
						}
					}
				})
				$("#productUnitPicker").html();				
			});
			
//	$("#qrygoodsClass").UIDropDialog( {
//		'triggerClass' : "idTreePicker",
//		'content' : $("#productUnitPicker")
//	});

	//"close"样式(-)是已经展开时的样式
	// 控制方法：
	// 如果当前节点尚未展开(+)，此次点击触发下列行为：(1)将当前节点样式设置为已展开；(2)从后台查数据，渲染子节点;(3)将同辈的其他已展开节点及其子节点收缩，且将收缩节点的样式设置为收缩样式
	// 如果当前节点已经展开(-)，此次点击触发下列行为：(1)将当前节点的子节点收缩；(2)当前节点的样式设置为展开(-)
	$(".category_trigger1").live(
			'click',
			function() {
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					category_triggerObj.parent(".tree_category1").next(".children").slideUp(function(){
						category_triggerObj.removeClass("close");
					});
					return false;
				}
				$(this).addClass("close");
				var pk_marbasclass = $(this).next("input").next().next().val();
				var myts = new Date();
				//var formStr = $('#prodclass_filter_form').serialize();
				var formStr = $(this).parents("form").find("#prodCl_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context
							+ "/other/qryMarClassByPk?pk_marbasclass="
							+ pk_marbasclass 
							+ "&myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						var orgArr = eval(result);
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
						var childrenDiv = $("<div></div>").addClass("children");
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];
							var prodClassPk = obj.pk_marbasclass;
							var prodClassName = obj.name;
							var a = $("<a></a>").attr("href", "#")
									.addClass("category_trigger1")
									.attr("style","margin-left:"+current_margin_left_size+"px")
									.attr("margin", current_margin_left_size);
							var inputObj = $("<input/>")
											.attr("type", "checkbox")
											.attr("id", "pk_marbasclass_intree")
											.attr("name", "pk_marbasclass_intree")
											.val(obj.pk_marbasclass+"_"+obj.name)
											.addClass(obj.pk_marbasclass);
							$(inputObj).click(function(){
								var checkValue = $(this).val();
								var checkProdClassPk = checkValue.substr(0,20);
								var checkProdClassName = checkValue.substr(21);
								if(this.checked){
									var checkBox = $("<input/>")
													.attr("type", "checkbox")
													.val(checkValue)
													.attr("style","margin-left:0px;px;margin-top:0px").attr("checked","checked");
									$(checkBox).click(function(){
										var checkValue = $(this).val();
										var checkProdClassPk = checkValue.substr(0,20);
										var checkProdClassName = checkValue.substr(21);
										var objs = $("."+checkProdClassPk);
										if(objs.length > 0){
											objs[0].checked = false;
										}
										$(this).parent().remove();
										var divHeight = $("#sel_prodclass")[0].offsetHeight;
										var allChild = $("#sel_prodclass").children();
										var lastChildHeight = allChild[allChild.length - 1].offsetTop;										
										//设置已选产品去高度
										var objDialogs = $(".ui-drop_dialog_content");
										var idxObj;
										for(idxObj=0;idxObj<objDialogs.length;idxObj++){
											var objDialog = $(objDialogs[idxObj]);
											if(objDialog.is(":visible")){
												objDialog.attr("style","height:"+ lastChildHeight +"px");
												break;
											}
										}
										$($(".border.lft")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
										$($(".border.rit")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
										$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
										$(".border.lft").attr("style","height:"+ lastChildHeight +"px");
										$(".border.rit").attr("style","height:"+ lastChildHeight +"px");
										$("#sel_prodclass").attr("style","height:"+ lastChildHeight +"px");										
									});
									var labelProd = $("<label />").html(checkProdClassName)
									var div = $("<div></div>").attr("id", checkProdClassPk).attr("name", checkProdClassPk).attr("style", "float:left;display:inline").append(checkBox).append(labelProd);				
									$("#sel_prodclass").append(div);;
								}else{									
									$("#"+checkProdClassPk).remove();
								}								
								var allChild = $("#sel_prodclass").children();
								var lastChildHeight = allChild[allChild.length - 1].offsetTop;									
								$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
				                $(".border.lft").attr("style","height:"+ lastChildHeight +"px");
				                $(".border.rit").attr("style","height:"+ lastChildHeight +"px");
				                $("#sel_prodclass").attr("style","height:"+ lastChildHeight +"px");	
							});
							//查询该物料是否已经被选择
							var objProdClass = $("#"+obj.pk_marbasclass);
							if(objProdClass.length > 0){
								inputObj.attr("checked", "checked");
							}
							var label = $("<label></label>").text(obj.name);
							var inputObj1 = $("<input/>")
											.val(obj.pk_marbasclass)
											.attr("type", "hidden");	
							var div = $("<div></div>")
									.addClass("tree_category1").append(a)
									.append(inputObj).append(label).append(inputObj1);
							childrenDiv.append(div);
						}
						childrenDiv.insertAfter(category_triggerObj.parent());
					}
				});
				
				//自身展开后，将其兄弟节点及其子节点收缩回
				category_triggerObj.parent(".tree_category1").next(".children").slideDown();
				category_triggerObj.parent(".tree_category1").siblings(".tree_category1").next(".children").slideUp(function(){
					 $(this).prev(".tree_category1").find(".category_trigger1").removeClass("close");
				 });
				 $("#productUnitPicker").html();
				return false;
			});
	
	$(".tree_btns1 .idBtnPicked").live('click', function() {
		var items = $("#sel_prodclass").find("[type='checkbox']");
		if(items != null){
			var str="";
			var pks="";
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){
					str += $(items[loop]).next("label").html() + ",";	
 					pks += $(items[loop]).val().substr(0,20) + ",";
				}
			}
			var length1=str.length;	
			var length2=pks.length;
			$("#qrygoodsClass").val(str.substring(0, length1-1));
			$("#pk_marbasclass").val(pks.substring(0, length2-1));
		}		
		//$(this).parents(".ui-drop_dialog").slideUp();
		$("#productUnitPicker").slideUp();
		return 0;
	});
	
	$(".tree_btns1 .idBtnCancel").live('click', function(){
		$("#prodclass_filter").val("");
		//$(this).parents(".ui-drop_dialog").slideUp();
		$("#productUnitPicker").slideUp();
	})

	function len(s) { 
		var l = 0; 
		var a = s.split(""); 
		for (var i=0;i<a.length;i++) { 
			if (a[i].charCodeAt(0)<299) { 
				l++; 
			} else { 
				l+=2; 
			} 
		} 
		return l; 
	}
}

$(function() {
	var gc = new GoodsClass();
});
